package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class MapTransferPlayer extends Actor {

    static final int IDLE = 0;
    static final int RUN = 1;

    static final int LEFT = -1;
    static final int RIGHT = 1;

    private Pixmap mPixmap;
    private Texture mTexture;
    private Animation bobIdleRight;
    private Animation bobIdleLeft;
    private Animation bobRight;
    private Animation bobLeft;

    int state = IDLE;
    int dir = LEFT;
    private float stateTime;

    public MapTransferPlayer() {
        mPixmap = new Pixmap(Gdx.files.internal("bob.png"));
        mTexture = new Texture(mPixmap);
        // 切割纹理
        TextureRegion[] split = new TextureRegion(mTexture).split(20, 20)[0];
        TextureRegion[] mirror = new TextureRegion(mTexture).split(20, 20)[0];
        // 水平镜像纹理区域
        for (TextureRegion region : mirror)
            region.flip(true, false);
        // 空闲时的动画组装
        bobIdleRight = new Animation(0.5f, split[0], split[4]);
        bobIdleLeft = new Animation(0.5f, mirror[0], mirror[4]);
        // 行走时的动画组装
        bobRight = new Animation(0.1f, split[0], split[1]);
        bobLeft = new Animation(0.1f, mirror[0], mirror[1]);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

        processKeys();

        TextureRegion textureRegion = null;
        Animation ani = null;
        if (state == IDLE) {
            if (dir == LEFT) {
                ani = bobIdleLeft;
            } else {
                ani = bobIdleRight;
            }
        } else {
            // 大于0.5秒就切换为空闲状态
            if (stateTime > 0.5f) {
                state = IDLE;
            }
            if (dir == LEFT) {
                ani = bobLeft;
            } else {
                ani = bobRight;
            }

            translate(20 * dir * Gdx.graphics.getDeltaTime(), 0);
        }

        textureRegion = ani.getKeyFrame(stateTime, true);

        batch.draw(textureRegion, getX(), getY(),
                textureRegion.getRegionWidth() / 2,
                textureRegion.getRegionHeight() / 2,
                textureRegion.getRegionWidth(),
                textureRegion.getRegionHeight(), getScaleX(), getScaleY(),
                getRotation());
        stateTime += Gdx.graphics.getDeltaTime();
    }

    private void translate(float v, int i) {
        Gdx.app.log("MapTransferPlayer", "translate v:" + v + ", i:" + i);
        setX(getX() + v);
    }

    private void processKeys() {
//        Gdx.app.log("MapTransferPlayer", "processKeys");
        float x0 = (Gdx.input.getX(0) / (float) Gdx.graphics.getWidth()) * 480;
        float x1 = (Gdx.input.getX(1) / (float) Gdx.graphics.getWidth()) * 480;

        boolean leftButton = (Gdx.input.isTouched(0) && x0 < 70)
                || (Gdx.input.isTouched(1) && x1 < 70);
        boolean rightButton = (Gdx.input.isTouched(0) && x0 > 70 && x0 < 140)
                || (Gdx.input.isTouched(1) && x1 > 70 && x1 < 140);

        if (leftButton) {
            dir = LEFT;
            if (state == IDLE) {
                state = RUN;
                stateTime = 0;
            }
        } else if (rightButton) {
            dir = RIGHT;
            if (state == IDLE) {
                state = RUN;
                stateTime = 0;
            }
        }
    }

    @Override
    public void clear() {

//        super.clear();
        mPixmap.dispose();
        mTexture.dispose();
    }
}
